package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService dartpartmentService;
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageResult<Department> result = dartpartmentService.query(qo);
        model.addAttribute("pageResult",result);
        return "department/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if(id != null){
            Department department = dartpartmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if(department.getId() == null){
            dartpartmentService.save(department);
        }else{
            dartpartmentService.update(department);
        }
        return "redirect:/department/list.do";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        System.out.println(id);
        try {
            if(id != null){
                dartpartmentService.delete(id);
            }
            return new JsonResult();
        }catch (Exception e){
            return new JsonResult(false,"删除失败");
        }
    }
}
